package com.ChatServer.ferradm.cs.newpaltz.edu;

import android.app.Activity;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import dcf.Component;
import dcf.Endpoint;
import dcf.InitializationException;
import dcf.LocationService;


public class ChatServerWindow extends Activity implements OnClickListener{
		
	/**Initialize the window variables */
  	String lsHost ;
  	int    lsPort      ;
	String serverHost  ;
	int    serverPort ;
	Endpoint lsEndpoint;
	Endpoint csEndpoint;
	SharedPreferences pref;
	Button stopServer;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.servergui);
		/** Initialize the different component*/
		stopServer=(Button) findViewById(R.id.stop);
		stopServer.setOnClickListener(this);
		SharedPreferences defaultManager=PreferenceManager.getDefaultSharedPreferences(this);
		lsHost = defaultManager.getString("lsHost", "default");
		lsPort  = Integer.decode(new String(defaultManager.getString("lsPort", "default")))    ;
		serverHost = defaultManager.getString("serverHost", "default");
		serverPort = Integer.decode(new String(defaultManager.getString("serverPort", "default")))    ;
		Toast.makeText(this,"lsHost :"+lsHost+" lsPort: "+lsPort+" serverHost: "+serverHost+" serverPort :"+serverPort , Toast.LENGTH_LONG).show();
		Log.i("ONSTART", "ONSTART OK");
		ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		Log.i("ONSTART", "connectivityManager OK");
		NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
		Log.i("ONSTART", "networkInfo OK");
		/**networkInfo can be null and crash the activity..*/
		if (networkInfo!=null){
			String networkType=networkInfo.getTypeName();
			Log.i("ONSTART", "networkType OK");
			State networkState=networkInfo.getState();
			Log.i("ONSTART", "getState OK");
			Log.i("ONSTART","Network info : "+networkInfo.getDetailedState().toString()+" "+networkInfo.getSubtypeName().toString());
			/**Check you are connected*/
			if (networkState.compareTo(State.CONNECTED)==0){
				
						/**Framework stuff */
						try {
							lsEndpoint = new Endpoint(lsHost, lsPort);
							csEndpoint = new Endpoint(serverHost, serverPort);	
							LocationService ls = new LocationService(lsEndpoint);
							ChatServerBehavior csBehavior = new ChatServerBehavior(lsEndpoint, csEndpoint);
							Component msgHandler = new Component(csEndpoint, csBehavior);	
							//DEBUG
							System.out.println("lsEndpoint : host "+lsEndpoint.getHost()+" port "+lsEndpoint.getPort()+" csEndpoint: host "+csEndpoint.getHost()+" port "+csEndpoint.getPort() );
						} catch (InitializationException e) {		
							e.printStackTrace();
						}
					}
					}
					else{
						Toast.makeText(this, "Unable to make the server, not connected", Toast.LENGTH_SHORT).show();
						Log.e("error","Not Connected !");		
					}	
    }

	@Override
	public void onClick(View v) {
		finish();	
	}	
}